package com.ali.Reggie.controller;

import com.ali.Reggie.common.R;
import com.ali.Reggie.dto.SetmealDto;
import com.ali.Reggie.entity.Category;
import com.ali.Reggie.entity.Setmeal;
import com.ali.Reggie.entity.SetmealDish;
import com.ali.Reggie.service.CategoryService;
import com.ali.Reggie.service.SetmealDishService;
import com.ali.Reggie.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author ：ALi
 * @date ：Created in 2022/6/12 14:33
 * @description：
 * @modified By：
 * @version: $
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("page")
    public R<Page> showAll(int page, int pageSize, String name){
        Page<Setmeal> page1 = new Page<>(page,pageSize);
        Page<SetmealDto> dtoPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        lambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(page1, lambdaQueryWrapper);
        BeanUtils.copyProperties(page1,dtoPage,"records");
        List<Setmeal> records = page1.getRecords();
        List<SetmealDto> list = new ArrayList<>();

        for (Setmeal record:records) {
            Long categoryId = record.getCategoryId();
            Category category = categoryService.getById(categoryId);

            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record,setmealDto);
            if(category != null){
                //分类名称
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
                list.add(setmealDto);
            }

        }
        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }

    //新增套餐的保存按钮
    @PostMapping()
    public R<String> SaveSetMeal(@RequestBody SetmealDto setmealDto){

        setmealService.saveWithDish(setmealDto);
        return R.success("保存成功");
    }

    //修改按钮
    @GetMapping("/{id}")
    public R<SetmealDto> updateSetMeal(@PathVariable Long id){
        //获取基本信息
//        Setmeal setmeal = setmealService.getById(id);
//        SetmealDto setmealDto = new SetmealDto();
//        BeanUtils.copyProperties(setmeal,setmealDto);
//        //获取菜品信息
//        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
//        List<SetmealDish> setmealDishList = setmealDishService.list(lambdaQueryWrapper);
//        setmealDto.setSetmealDishes(setmealDishList);
        //以上的抽为方法了，就是updateWithDish
        SetmealDto setmealDto = setmealService.updateWithDish(id);
        return R.success(setmealDto);
    }

    //删除和批量删除
    @DeleteMapping()
    @Transactional
    public R<String> deleteById(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return R.success("删除成功!");
    }

    //修改状态
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable("status") int status,@RequestParam("ids") List<Long> ids){
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(ids != null,Setmeal::getId,ids);
        List<Setmeal> setmealList = setmealService.list(lambdaQueryWrapper);
        for (Setmeal setmeal:setmealList) {
            setmeal.setStatus(status);
        }
        setmealService.updateBatchById(setmealList);
        return R.success("状态修改成功");
    }

    //修改内容后保存
    @PutMapping
    public R<String> saveByid(@RequestBody SetmealDto setmealDto){
        setmealService.UpdateSaveWithDish(setmealDto);
        return R.success("保存成功");
    }

    //客户端使用的套餐按钮
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        lambdaQueryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        lambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmealList = setmealService.list(lambdaQueryWrapper);
        return R.success(setmealList);
    }

}